<?php

namespace App\Admin\Controllers;

use App\Models\Banner;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use \Log;
use \Cache;

class BannerController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('轮播信息管理');
            $content->description('轮播信息列表');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('轮播信息管理');
            $content->description('编辑轮播信息');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('轮播信息管理');
            $content->description('编辑轮播信息');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('轮播信息管理');
            $content->description('添加轮播信息');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Banner::class, function (Grid $grid) {
            $grid->id('轮播信息ID')->sortable();
            $grid->name('轮播信息名称');
            $grid->img('轮播信息图片')->image(env('BASE_URL'),100,100);

            $grid->type('位置')->display(function ($released) {
                $res = true;
                if ($released == 1) {
                    $res = '左侧轮播图';
                }
                if ($released == 2) {
                    $res = 'icon';
                }
                if ($released == 3) {
                    $res = '右侧轮播图';
                }
                if ($released == 4) {
                    $res = '新闻列表';
                }
                return $res;
            })->sortable();
            $grid->sort('排序(序号小的在前)')->sortable();
            $grid->inttime('开始时间');
            $grid->endtime('结束时间');

            $grid->filter(function ($filter) {
                // 在这里添加字段过滤器
                $filter->equal('id', '轮播信息ID');
                $filter->equal('type', '位置')->radio([
                    1 => '左侧轮播图',
                    2 => 'icon',
                    3 => '右侧轮播图',
                    4 => '新闻列表',
                ]);
                $filter->between('inttime', '开始时间')->datetime();
                $filter->between('endtime', '结束时间')->datetime();

            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Banner::class, function (Form $form) {

            $form->display('id', '轮播信息ID');
            $form->text('name','轮播信息名称');
            $form->image('img','轮播信息图片')->uniqueName();
            $form->multipleSelect('type','轮播信息位')->options([
                1 => '左侧轮播图',
                2 => 'icon',
                3 => '右侧轮播图',
                4 => '新闻列表',
            ])->required();
            $form->text('value','网址');
            $form->datetime('inttime','开始时间')->required();
            $form->datetime('endtime','结束时间')->required();
            $form->number('sort','排序(序号小的在前)')->default(100);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');

        });
    }
}
